package com.example.demo.util;

import java.util.List;

// 计算汉明距离和相似度的工具类
class ImageSimilarity {
    // 计算汉明距离
    public static int hammingDistance(String hash1, String hash2) {
        if (hash1.length() != hash2.length()) {
            throw new IllegalArgumentException("两个哈希值长度必须相同");
        }
        int distance = 0;
        for (int i = 0; i < hash1.length(); i++) {
            if (hash1.charAt(i) != hash2.charAt(i)) {
                distance++;
            }
        }
        return distance;
    }

    // 计算图片相似度
    public static double calculateSimilarity(String hash1, String hash2) {
        int distance = hammingDistance(hash1, hash2);
        int totalBits = hash1.length();
        return 1.0 - (double) distance / totalBits;
    }

    // 综合多个尺度的相似度结果
    public static double multiScaleSimilarity(List<String> hashes1, List<String> hashes2) {
        double totalSimilarity = 0;
        for (int i = 0; i < hashes1.size(); i++) {
            double similarity = ImageSimilarity.calculateSimilarity(hashes1.get(i), hashes2.get(i));
            totalSimilarity += similarity;
        }
        return totalSimilarity / hashes1.size();
    }
}
